import 'package:aero_frisbee/app/conf/af_app_key.dart';
import 'package:aero_frisbee/app/state/app_cache_state.dart';
import 'package:aero_frisbee/pages/entry/af_onboarding_page.dart';
import 'package:aero_frisbee/pages/entry/app_launch_page.dart';
import 'package:aero_frisbee/pages/login/af_login_page.dart';
import 'package:aero_frisbee/pages/modues/message/pages/message/message_detail/af_message_detail_screen.dart';
import 'package:aero_frisbee/pages/modues/mine/profile/details/af_block_list_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:talker_flutter/talker_flutter.dart';

import 'app/model/local_post_model/local_post_model.dart';
import 'pages/modues/message/pages/web_view_page.dart';
import 'pages/modues/mine/profile/details/af_about_screen.dart';
import 'pages/modues/mine/profile/details/af_customer_service_screen.dart';
import 'pages/modues/mine/profile/details/af_setting_screen.dart';
import 'pages/modues/mine/purchase/af_app_purchase_screen.dart';
import 'pages/modues/post/af_create_post/af_create_post_screen.dart';
import 'pages/modues/post/af_post_detail_screen.dart';
import 'pages/modues/post/af_profile_detail_screen/af_profile_detail_screen.dart';

final GlobalKey<NavigatorState> appNavigatorKey = GlobalKey();

class RouteUri {
  static const String launch = '/';
  static const String toLaunch = '/app/launch';
  static const String logout = '/logout';
  static const String login = '/login';
  static const String about = '/AfAbout';
  static const String blockList = '/AfBlockListScreen';
  static const String setting = '/AfSettingScreen';
  static const String customerService = '/AfCustomerServiceScreen';
  static const String postDetail = '/AfPostDetailScreen';
  static const String messageDetai = '/AfMessageDetailScreen';
  static const String profileDetailScreen = '/AfProfileDetailScreen';
  static const String appPurchase = '/AfAppPurchaseScreen';
  static const String afCreatePost = '/AfCreatePostScreen';
  static const String webViewPage = '/webViewPage';
}

final appRouterProvider = Provider<GoRouter>((ref) {
  final talker = TalkerFlutter.init();
  return GoRouter(
    observers: [TalkerRouteObserver(talker)],
    initialLocation: RouteUri.launch,
    navigatorKey: appNavigatorKey,
    errorPageBuilder: (context, state) => NoTransitionPage<void>(
      key: state.pageKey,
      child: const SizedBox(
        height: 20,
      ),
    ),
    routes: [
      GoRoute(
        path: RouteUri.launch,
        builder: (context, state) {
          final prefs = ref.read(appPrefsProvider);
          final isShowPa = prefs.getBool(AfAppKey.isShowOnboardingKey);
          if (isShowPa == true) {
            return const AppLaunchPage();
          } else {
            return const AFOnboardingPage();
          }
        },
      ),
      GoRoute(
        path: RouteUri.toLaunch,
        name: RouteUri.toLaunch,
        builder: (context, state) {
          final prefs = ref.read(appPrefsProvider);
          prefs.setBool(AfAppKey.isShowOnboardingKey, true);
          return const AppLaunchPage();
        },
      ),
      GoRoute(
        path: RouteUri.about,
        name: RouteUri.about,
        builder: (context, state) => const AfAboutScreen(),
      ),
      GoRoute(
        path: RouteUri.blockList,
        name: RouteUri.blockList,
        builder: (context, state) => const AfBlockListScreen(),
      ),
      GoRoute(
        path: RouteUri.setting,
        name: RouteUri.setting,
        builder: (context, state) => const AfSettingScreen(),
      ),
      GoRoute(
        path: RouteUri.customerService,
        name: RouteUri.customerService,
        builder: (context, state) => const AfCustomerServiceScreen(),
      ),
      GoRoute(
        path: RouteUri.postDetail,
        name: RouteUri.postDetail,
        builder: (context, state) =>
            AfPostDetailScreen(item: state.extra as LocalPostModel),
      ),
      GoRoute(
        path: RouteUri.profileDetailScreen,
        name: RouteUri.profileDetailScreen,
        builder: (context, state) =>
            AfProfileDetailScreen(item: state.extra as LocalPostModel),
      ),
      GoRoute(
        path: RouteUri.login,
        name: RouteUri.login,
        builder: (context, state) => const AFLoginPage(),
      ),
      GoRoute(
        path: RouteUri.messageDetai,
        name: RouteUri.messageDetai,
        builder: (context, state) => AfMessageDetailScreen(
          userId: state.queryParams['userId'] as String,
        ),
      ),
      GoRoute(
        path: RouteUri.appPurchase,
        name: RouteUri.appPurchase,
        builder: (context, state) => const AfAppPurchaseScreen(),
      ),
      GoRoute(
        path: RouteUri.afCreatePost,
        name: RouteUri.afCreatePost,
        builder: (context, state) => const AfCreatePostScreen(),
      ),
      GoRoute(
        name: RouteUri.webViewPage,
        path: RouteUri.webViewPage,
        builder: (context, state) {
          final title = state.queryParams['title'] ?? "";
          final url = state.queryParams['url'] ?? "";
          final content = state.queryParams['content'] ?? "";
          return WebViewPage(
            title: title,
            url: url,
            content: content,
          );
        },
      )
    ],
    redirect: (BuildContext context, GoRouterState state) {
      return null;
    },
  );
});
